// pages/cart/cart.js
var app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        total:0,
        discount:0,
        allChecked:false,
        Mstart:'',
        currentIndex: 0, 
        show:false,
        product:{
            picUrl:[['../../images/1.png']],
            price:[[1,2,3],[4,5,6],[7,8,9],[10,11,12]],
            stock:50000,
            selected_size:0,
            selected_flavor:0,
            size:['220克/罐','礼盒装(4罐)','100克/袋','100克/4袋(包邮)'],
            flavor:['酸甜口味','偏甜','偏酸'],
            num:1
        },
        slideButtons:[
            {
                text: '删除',
                extClass: 'test',
                src:'/images/ic_delete.png'
            }
        ],
        cartList:[
            {
                id:0,
                name:"追源深山土蜂蜜",
                picUrl:"../../images/1.png",
                size:"一斤装",
                price:88.00,
                num:1,
                checked:false
            },
            {
                id:1,
                name:"追源深山土蜂蜜",
                picUrl:"../../images/1.png",
                size:"二斤装",
                price:88.00,
                num:1,
                checked:true
            },
            {
                id:0,
                name:"追源深山土蜂蜜",
                picUrl:"../../images/1.png",
                size:"一斤装",
                price:88.00,
                num:1,
                checked:false
            },
            {
                id:0,
                name:"追源深山土蜂蜜",
                picUrl:"../../images/1.png",
                size:"一斤装",
                price:88.00,
                num:1,
                checked:false
            },
            {
                id:0,
                name:"追源深山土蜂蜜",
                picUrl:"../../images/1.png",
                size:"一斤装",
                price:88.00,
                num:1,
                checked:false
            },
            {
                id:0,
                name:"追源深山土蜂蜜",
                picUrl:"../../images/1.png",
                size:"一斤装",
                price:88.00,
                num:1,
                checked:false
            }
        ]

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let shareid = options.shareid;

        if (shareid){
            wx.setStorageSync('shareid', shareid);
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        let cartlist = this.data.cartList;
        let total = 0;

        for(let i in cartlist){
            if (cartlist[i].checked === true){
                total += cartlist[i].price;
            }
        }
        this.setData({
            show:false,
            total:total
        })

        
        console.log(this.data.show);
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            show:false
        })

        let userinfo = wx.getStorageSync('userInfo');
        let cartlist = this.data.cartList;

        if (!userinfo){
            wx.showModal({
                title:'提示',
                content:'您还没有登录',
                success(res){
                    if (res.confirm){
                        // wx.switchTab({
                        //   url: '/pages/ucenter/index/index',
                        // })
                        app.getuserInfo();
                    } else if (res.cancel){
                        wx.switchTab({
                            url: '/pages/index/index',
                        })
                    }
                }
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title:'sharetest',
            path:'/pages/index/index?shareid=' + '54185418'
        }
    },
    /**
     * (增加数量)
     * @param {*} e
     * @徐志鹏
     * @2021-1-28 
     */
    addNum:function (e){
        let index = e.currentTarget.dataset.index;
        let cartlist = this.data.cartList;
        cartlist[index].num++;

        this.setData({
            cartList:cartlist,
        })
        this.sum();
    },
    /**
    * (减少数量)
    * @param {*} e
    * @徐志鹏
    * @2021-1-28 
    */
    subNum:function (e){
        let index = e.currentTarget.dataset.index;
        let cartlist = this.data.cartList;

        if (cartlist[index].num === 1){
            wx.showToast({
                title: '不能再减了!',
                icon:'none'
              })
        }else {
            cartlist[index].num--;
        }
    
        this.setData({
            cartList:cartlist,
        })

        this.sum();
    },
    /**
     * (总计价格)
     * @param {*} e
     * @徐志鹏
     * @2021-1-29 
     */
    sum:function(){
        let total = 0;
        let cartlist = this.data.cartList;

        for (let i of cartlist){
            if (i.checked === true){
                total += i.price * i.num;
            }
        }

        this.setData({
            total:total
        })
    },
    /**
     * (复选框事件)
     * @param {*} e
     * @徐志鹏
     * @2021-1-29
     */
    checkboxChange:function(e){
        let that = this;
        let allCheck = this.data.allChecked;
        let index = e.currentTarget.dataset.index;
        let cartlist = this.data.cartList;

        if (cartlist[index].checked === true){
            cartlist[index].checked = false;
            allCheck = false;
        }else{
            allCheck = true;
            cartlist[index].checked = true;
            cartlist.forEach((item)=>{
                if (!item.checked)
                    allCheck = false;
            })
        }

        this.setData({
            cartList:cartlist,
            allChecked:allCheck
        })
        this.sum();

    },
    /**
     * (全选事件)
     * @param {*} e
     * @徐志鹏
     * @2021-1-29
     */
    checkAll:function(e){
        let that = this;
        let allchecked = e.currentTarget.dataset.allchecked;
        let cartlist = this.data.cartList;
        let total = 0;

        if (allchecked === true){
            for (let i of cartlist){
                i.checked = false;
            }
            
            console.log(cartlist);

            this.setData({
                cartList:cartlist,
                total:0,
                allChecked:false
            })
        }else{
            for (let i of cartlist){
                i.checked = true;
                total += i.price * i.num;
            }
    
            this.setData({
                allChecked:true,
                cartList:cartlist,
                total:total
            })
        }   
    },
    /**
     * (滑动删除开始事件)
     * @param {*} e
     * @徐志鹏
     * @2021-1-30
     */
    touchstart:function(e){
        this.setData({
            Mstart:e.changedTouches[0].pageX
        })
    },
     /**
     * (滑动删除正在进行事件)
     * @param {*} e
     * @徐志鹏
     * @2021-1-30
     */
    touchmove:function(e){
        let move = this.data.Mstart - e.changedTouches[0].pageX;
        let cartlist = this.data.cartList;
        let index = e.currentTarget.dataset.index;
        
        cartlist[index].left = move > 0 ? -move : 0;

        this.setData({
            cartList:cartlist
        })
       
    },
     /**
     * (滑动删除结束事件)
     * @param {*} e
     * @徐志鹏
     * @2021-1-30
     */
    touchend:function(e){
        let lastMove = this.data.Mstart - e.changedTouches[0].pageX;
        let cartlist = this.data.cartList;
        let index = e.currentTarget.dataset.index;
        let animation = '';

        if (lastMove > 90){
            animation = this.slideAnimation(-180,400);
        }else {
            animation = this.slideAnimation(0,400);
        }

        cartlist[index].animation = animation;

        this.setData({
            cartList:cartlist
        })
    },
     /**
     * (滑动动画)
     * @param {*} recordX time
     * @徐志鹏
     * @2021-1-30
     */
    slideAnimation(recordX, time) {
        let animation = wx.createAnimation({
            timingFunction: 'ease'
        });

        animation.translate(recordX + 'rpx', 0).step();

        return animation.export();
    },
    /**
     * (滑动删除)
     * @param {*} e
     * @徐志鹏
     * @2021-1-30
     */
    del:function(e){
        let that = this;
        let index = e.currentTarget.dataset.index;
        let cartlist = that.data.cartList;

        cartlist.splice(index,1);
        this.setData({
            cartList:cartlist
        })

        this.sum();
    },
    /**
     * (弹出层关闭事件)
     * @param {*} e
     * @徐志鹏
     * @2021-1-30
     */
    onClose:function(e){
        this.setData({
            show:false
        })
        
        wx.showTabBar({
          animation: true,
        });
        console.log(this.data.show);
    },
    /**
     * (弹出层事件)
     * @param {*} e
     * @徐志鹏
     * @2021-1-30
     */
    changeShow:function(e){
        let index = e.currentTarget.dataset.index;
        let cartlist = this.data.cartList;
        let product = this.data.product;
        
        product.index = index;
        product.picUrl = cartlist[index].picUrl;
        product.num = cartlist[index].num;
       // product.price = cartlist[index].price;
        product.selected = cartlist[index].size;
        product.name = cartlist[index].name;

        this.setData({
            show:true,
            product:product
        })  

        wx.hideTabBar({
            animation: true,
        });
        console.log(this.data.show);
    },
    /**
     * (弹出层更改商品规格事件)
     * @param {*} e
     * @徐志鹏
     * @2021-1-31
     */
    change_size:function(e){
        let index = e.currentTarget.dataset.index;
        let product = this.data.product;

        product.selected_size = index;

        this.setData({
            product:product
        })
    },
    /**
     * (弹出层更改商品规格事件)
     * @param {*} e
     * @徐志鹏
     * @2021-1-31
     */
    change_flavor:function(e){
        let index = e.currentTarget.dataset.index;
        let product = this.data.product;

        product.selected_flavor = index;

        this.setData({
            product:product
        })
    },
     /**
     * (弹出层更改商品数量减事件)
     * @徐志鹏
     * @2021-1-31
     */
    c_subNum:function(){
        let product = this.data.product;

        if (product.num === 1){
            wx.showToast({
                title: '不能再减了!',
                icon:'none'
            });
        }else {
            product.num--;
        }

        this.setData({
            product:product
        })
    },
     /**
     * (弹出层更改商品数量加事件)
     * @徐志鹏
     * @2021-1-31
     */
    c_addNum:function(){
        let product = this.data.product;

        product.num++;

        this.setData({
            product:product
        })
    },
     /**
     * (弹出层确定按钮事件)
     * @徐志鹏
     * @2021-1-31
     */
    determine:function(){
        let cartlist = this.data.cartList;
        let product = this.data.product;

        cartlist[product.index].id = product.index;
        cartlist[product.index].picUrl = product.picUrl;
        cartlist[product.index].size = product.size[product.selected_size] + ' / ' + product.flavor[product.selected_flavor];
        cartlist[product.index].price = product.price[product.selected_size][product.selected_flavor];
        cartlist[product.index].num = product.num;
        
        
        this.setData({
            cartList:cartlist
        })
        this.onClose();
    },
    /**
     * (结算按钮事件)
     * @param {*} e 
     * @徐志鹏
     * @2021-2-10
     */
    order:function(e){
        let cartlist = this.data.cartList;
        let orderlist = [];

        for (let i of cartlist){
            if (i.checked)
                orderlist.push(i);
        }
        console.log(orderlist);

        wx.setStorageSync('orderList', orderlist);
        wx.navigateTo({
          url: '/pages/order/order',
        })
    },
    bindshow:function(e){
        let index = e.currentTarget.dataset.index;
        let cartlist = this.data.cartList;

        cartlist[index].show = true;

        this.setData({
            cartList:cartlist
        })
    }
})